﻿package com.vicentediaz.data
{
    import com.vicentediaz.data.events.*;
    import flash.events.*;
    import flash.media.*;
    import flash.net.*;
    import flash.utils.*;

    public class Sounds extends EventDispatcher
    {
        private var verbose:Boolean = true;
        private var sounds:Array;
        private var _totalSounds:uint;
        private var _xml:XML;
        private var ids:Dictionary;
        private var nextSound:int;
        private var allLoadedDispatched:Boolean;
        private static var instance:Sounds;

        public function Sounds(param1:IEventDispatcher = null)
        {
            super(param1);
            return;
        }// end function

        public function get totalSounds() : uint
        {
            return this._xml.sound.length();
        }// end function

        public function get loadedSounds() : uint
        {
            return this.nextSound;
        }// end function

        private function say(param1:String) : void
        {
            if (this.verbose)
            {
                trace("<< SOUNDS : " + param1 + " >>");
            }// end if
            return;
        }// end function

        public function playById(param1:String, param2:Number = 1, param3:Number = 0) : void
        {
            var _loc_4:* = this.ids[param1];
            var _loc_5:* = new SoundTransform(param2, param3);
            var _loc_6:* = _loc_4.play(0, 0, _loc_5);
            return;
        }// end function

        public function loadSounds(param1:XML) : void
        {
            this._xml = param1;
            this._totalSounds = param1.sound.length();
            this.ids = new Dictionary();
            this.sounds = new Array();
            var _loc_2:* = Number(Config.getInstance().getByValue("SIMULTANEOUS_SOUNDS_DOWNLOADS"));
            _loc_2 = _loc_2 < this._totalSounds ? (_loc_2) : (this._totalSounds);
            this.allLoadedDispatched = false;
            this.nextSound = 0;
            var _loc_3:uint;
            while (_loc_3++ < _loc_2)
            {
                // label
                this.loadNextSound();
            }// end while
            dispatchEvent(new GalleryEvent(GalleryEvent.START_TO_LOAD));
            return;
        }// end function

        public function getById(param1:String) : Sound
        {
            return this.ids[param1];
        }// end function

        private function onSoundLoaded(param1:Event) : void
        {
            var _loc_3:SoundsLoaderEvent;
            var _loc_2:* = new GalleryEvent(GalleryEvent.NEXT_THUMB_LOADED);
            _loc_2.totalThumbs = this._totalSounds;
            _loc_2.currentThumbs = this.nextSound;
            dispatchEvent(_loc_2);
            if (this.nextSound < this.totalSounds)
            {
                this.loadNextSound();
            }
            else if (!this.allLoadedDispatched)
            {
                _loc_3 = new SoundsLoaderEvent(SoundsLoaderEvent.ALL_SOUNDS_LOADED);
                _loc_3.totalSounds = this._totalSounds;
                this.allLoadedDispatched = true;
                dispatchEvent(_loc_3);
            }// end else if
            return;
        }// end function

        private function loadNextSound() : void
        {
            var _loc_1:* = new Sound();
            this.sounds.push(_loc_1);
            this.ids[this._xml.sound[this.nextSound].@id.toString()] = _loc_1;
            _loc_1.load(new URLRequest(Config.getInstance().getByValue("SOUNDS_PATH") + this._xml.sound[this.nextSound]));
            this.say("Cargando " + Config.getInstance().getByValue("SOUNDS_PATH") + this._xml.sound[this.nextSound]);
            _loc_1.addEventListener(Event.COMPLETE, this.onSoundLoaded, false, 0, true);
            var _loc_2:String;
            _loc_2.nextSound = this.nextSound++;
            return;
        }// end function

        public static function getInstance() : Sounds
        {
            instance = instance ? (instance) : (new Sounds);
            return instance;
        }// end function

    }
}
